<template>
  <!-- 标题按钮 -->
  <el-row>
    <el-col :span="12">
      <p
        style="
          font-size: 18px;
          margin-top: 20px;
          color: black;
          font-family: '楷体', serif;
        "
      >
        权益类型
      </p>
    </el-col>
    <el-col :span="12" align="right">
      <<el-button
        color="skyblue"
        :dark="isDark"
        @click="dialogFormVisible = true"
        plain
        >编辑权益</el-button
      >
    </el-col>
  </el-row>

  <!-- 权益信息 -->
  <el-card style="width: 100%">
    <el-row>
      <el-col :span="12">
        <table>
          <tr>
            <td>权益名称：{{ form.EName }}</td>
          </tr>
          <tr>
            <td>权益描述：{{ form.EDescription }}</td>
          </tr>
        </table>
      </el-col>
      <el-col :span="12">
        <table>
          <tr>
            <td>权益状态：</td>
            <td v-if="form.EStatus == true">生效</td>
            <td v-if="form.EStatus == false">已失效</td>
          </tr>
        </table>
      </el-col>
    </el-row>
  </el-card>

  <!-- 编辑权益弹框操作 -->
  <el-dialog v-model="dialogFormVisible" title="权益设置" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="upd"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="权益名称" prop="EName">
        <el-input v-model="upd.EName" />
      </el-form-item>

      <el-form-item label="权益状态">
        <el-switch v-model="upd.EStatus" />
      </el-form-item>

      <el-form-item label="权益说明" prop="EDescription">
        <el-input v-model="upd.EDescription" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="UpdEquity(ruleFormRef)">
          修 改
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import axios from "axios";
import type { FormInstance, FormRules } from "element-plus";
import { ElTable } from "element-plus";
import { ElMessage } from "element-plus";

axios.defaults.baseURL = "http://localhost:5089/api/";

// 系统路由
const router = useRouter();
// 当前页面路由
const route = useRoute();

const ruleFormRef = ref<FormInstance>();
const dialogFormVisible = ref(false);

//权益数据
const form = reactive({
  EId: 0,
  EName: "",
  EDescription: "",
  EStatus: true,
  EIcon: "",
  AddTime: "",
  TenantId: "default",
});

//权益数据
const upd = reactive({
  EId: 0,
  EName: "",
  EDescription: "",
  EStatus: true,
  EIcon: "",
  AddTime: "",
  TenantId: "default",
});

//权益校验
const rules = reactive<FormRules>({
  EName: [
    { required: true, message: "请输入权益名称", trigger: "blur" },
    { min: 2, max: 20, message: "权益名称长度介于2-20之间", trigger: "blur" },
  ],
  EDescription: [
    { required: true, message: "请输入权益说明", trigger: "blur" },
    { min: 2, max: 30, message: "权益说明长度介于2-20之间", trigger: "blur" },
  ],
});

// 获取权益数据
const GetEquityData = () => {
  let eId = Number(route.query.eId);

  console.log(eId);

  axios.get("Equities/GetEquitiesDelital?eId=" + eId).then((res) => {
    let d = res.data;
    form.EId = d.EId;
    form.EName = d.EName;
    form.EDescription = d.EDescription;
    form.EStatus = d.EStatus;
    form.EIcon = d.EIcon;
    form.AddTime = d.AddTime;
    form.TenantId = d.TenantId;
    console.log(res.data);
  });
};

// 修改自定义权益
const UpdEquityData = () => {
  let eId = Number(route.query.eId);

  console.log(eId);

  axios.get("Equities/GetEquitiesDelital?eId=" + eId).then((res) => {
    let d = res.data;
    upd.EId = d.EId;
    upd.EName = d.EName;
    upd.EDescription = d.EDescription;
    upd.EStatus = d.EStatus;
    upd.EIcon = d.EIcon;
    upd.AddTime = d.AddTime;
    upd.TenantId = d.TenantId;
    console.log(res.data);
  });
};

// 修改自定义权益
const UpdEquity = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      axios.post("Equities/UpdEquities", upd).then((res) => {
        if (res.data.Code == 200) {
          ElMessage.success("修改成功！");
          router.push("MemberEquity");
        } else {
          ElMessage.error("修改失败！");
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 取消
const resetForm = (formEl: FormInstance | undefined) => {
  dialogFormVisible.value = false;
};

onMounted(() => {
  GetEquityData();
  UpdEquityData();
});
</script>

<style lang="scss">
.td {
  width: 120px;
}
</style>
